package apis

import (
	"k8sdev/pkg/client"
	"log"
	"strings"

	"github.com/gin-gonic/gin"
	"k8s.io/apimachinery/pkg/util/proxy"
	"k8s.io/client-go/rest"
)

func Proxy(c *gin.Context) {
	err := client.NewClient()
	if err != nil {
		log.Fatal(err.Error())
		return
	}
	restConfig := client.GetRestConfig()

	// 创建一个transport
	t, err := rest.TransportFor(restConfig)
	if err != nil {
		log.Fatal(err.Error())
		return
	}

	s := *c.Request.URL
	s.Path = strings.Replace(s.Path, "/proxy", "", 1)
	s.Host = "192.168.145.25:6443"
	s.Scheme = "https"

	httpProxy := proxy.NewUpgradeAwareHandler(&s, t, true, false, nil)
	httpProxy.UpgradeTransport = proxy.NewUpgradeRequestRoundTripper(t, t)
	httpProxy.ServeHTTP(c.Writer, c.Request)
}
